package ru.spb.osll.myfit.server;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;

public class RequestProcessServletTest extends EasyMockSupport {
	private HttpServletRequest req;
	private HttpServletResponse res;
	private FilterConfig filterConfig;
	private ServletContext context;
	private RequestDispatcher dispatch;
	private RequestProcessServlet requestServlet;
	private FilterChain chain;

	@Before
	public void setUp() throws Exception {
		req = createStrictMock(HttpServletRequest.class);
		res = createStrictMock(HttpServletResponse.class);
		filterConfig = createStrictMock(FilterConfig.class);
		context = createStrictMock(ServletContext.class);
		dispatch = createStrictMock(RequestDispatcher.class);
		chain = createStrictMock(FilterChain.class);
		requestServlet = new RequestProcessServlet();
	}

	@Test
	public void testDoFilterRoot() throws Exception {

		EasyMock.expect(req.getMethod()).andReturn("GET");
		EasyMock.expect(req.getRequestURI()).andReturn("/");

		res.setHeader("Pragma", "no-cache");
		EasyMock.expectLastCall();
		res.setHeader("Cache-Control", "must-revalidate");
		EasyMock.expectLastCall();
		res.setHeader("Cache-Control", "no-cache");
		EasyMock.expectLastCall();
		res.setHeader("Cache-Control", "no-store");
		EasyMock.expectLastCall();
		res.setDateHeader("Expires", 0);
		EasyMock.expectLastCall();
		EasyMock.expect(filterConfig.getServletContext()).andReturn(context);
		context.setAttribute("pagePath", "");
		EasyMock.expectLastCall();
		EasyMock.expect(req.getRequestDispatcher("")).andReturn(dispatch);
		dispatch.forward(req, res);
		EasyMock.expectLastCall();

		requestServlet.init(filterConfig);

		replayAll();
		requestServlet.doFilter(req, res, null);
		verifyAll();
	}

	@Test
	public void testDoFilterTestPage() throws Exception {

		EasyMock.expect(req.getMethod()).andReturn("GET");
		EasyMock.expect(req.getRequestURI()).andReturn("/TestSuite.TestPage");

		res.setHeader("Pragma", "no-cache");
		EasyMock.expectLastCall();
		res.setHeader("Cache-Control", "must-revalidate");
		EasyMock.expectLastCall();
		res.setHeader("Cache-Control", "no-cache");
		EasyMock.expectLastCall();
		res.setHeader("Cache-Control", "no-store");
		EasyMock.expectLastCall();
		res.setDateHeader("Expires", 0);
		EasyMock.expectLastCall();
		EasyMock.expect(filterConfig.getServletContext()).andReturn(context);
		context.setAttribute("pagePath", "TestSuite.TestPage");
		EasyMock.expectLastCall();
		EasyMock.expect(req.getRequestDispatcher("")).andReturn(dispatch);
		dispatch.forward(req, res);
		EasyMock.expectLastCall();

		requestServlet.init(filterConfig);

		replayAll();
		requestServlet.doFilter(req, res, null);
		verifyAll();
	}

	@Test
	public void testDoFilterHtml() throws Exception {

		EasyMock.expect(req.getMethod()).andReturn("GET");
		EasyMock.expect(req.getRequestURI()).andReturn("/fitnesse.html");

		chain.doFilter(req, res);
		EasyMock.expectLastCall();

		requestServlet.init(filterConfig);

		replayAll();
		requestServlet.doFilter(req, res, chain);
		verifyAll();
	}

}
